package com.wgl.yygh.serivce.impl;

import com.alibaba.fastjson.JSONObject;
import com.wgl.cmnclient.inteface.DictFeignClient;
import com.wgl.model.hosp.Department;
import com.wgl.model.hosp.Hospital;
import com.wgl.model.vo.hosp.HospitalQueryVo;
import com.wgl.yygh.repository.HospitalRepository;
import com.wgl.yygh.result.Result;
import com.wgl.yygh.serivce.HospitalService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wgl
 * @version 1.0.0
 * @ClassName HospitalSeriveImpl.java
 * @Description TODO
 * @createTime 2022年02月05日 21:43:00
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Resource
    private HospitalRepository hospitalRepository;

    @Resource
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> map) {
        //先判断是否存在相同的数据 存在及修改  不存在就添加
        String s = JSONObject.toJSONString(map);
        Hospital hospital = JSONObject.parseObject(s, Hospital.class);

        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospitalExist != null){
            //修改
             hospital.setStatus(hospitalExist.getStatus());
             hospital.setCreateTime(hospitalExist.getCreateTime());
             hospital.setUpdateTime(new Date());
             hospital.setIsDeleted(0);
             hospitalRepository.save(hospital);
        } else {
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(String hospcode) {

        return hospitalRepository.getHospitalByHoscode(hospcode);
    }

    @Override
    public Page selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建分页参数Pageable
        Pageable pageable = PageRequest.of(page-1,limit);
        //
        ExampleMatcher matcher =  ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        Example<Hospital> example = Example.of(hospital,matcher);
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        //得到数据集合
        pages.getContent().stream().forEach(hospital1 ->{
            this.setHospitalHosType(hospital1);
        });
        return pages;

    }

    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String,Object> getHospById(String id) {
        //查询医院
        Hospital hospital = hospitalRepository.findById(id).get();
        //设置医院类型  这里没写返回值
        setHospitalHosType(hospital);
        Map<String,Object> map = new HashMap<>();
        //设置医院信息
        map.put("hospital",hospital);
        //设置医院的挂号规则
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return map;

    }

    @Override
    public String getHosName(String s) {
        Hospital byHoscode = getByHoscode(s);

        return  byHoscode == null?"":byHoscode.getHosname();
    }

    @Override
    public List<Hospital> findByHosName(String hosname) {

        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        //查询医院
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        //设置医院类型  这里没写返回值
        setHospitalHosType(hospital);
        Map<String,Object> map = new HashMap<>();
        //设置医院信息
        map.put("hospital",hospital);
        //设置医院的挂号规则
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return map;
    }

    /**
     * @Author: Wgl
     * @Description: 设置医院的类型
     * @Date: 2022/2/6 22:16;
     * @param hospital: ;
     * @return: void;
     */
    private void setHospitalHosType(Hospital hospital) {
        StringBuffer stringBuilder = new StringBuffer("");
       //拼接查询省市区地址
        stringBuilder.append(dictFeignClient.getName( hospital.getProvinceCode()).getData());
        stringBuilder.append("-");
        stringBuilder.append(dictFeignClient.getName( hospital.getCityCode()).getData());
        stringBuilder.append("-");
        stringBuilder.append(dictFeignClient.getName( hospital.getDistrictCode()).getData());
        stringBuilder.append("-");
        stringBuilder.append(hospital.getAddress());

        //查医院等级
        hospital.getParam().put("hostypeString",dictFeignClient.getName("Hostype", hospital.getHostype()).getData());

        hospital.getParam().put("fullAddress",stringBuilder.toString());


    }
}
